package com.twt.survey.controller;

import cn.hutool.core.convert.ConverterRegistry;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.twt.survey.pojo.Questionnaire;
import com.twt.survey.pojo.QuestionnaireDTO;
import com.twt.survey.pojo.SignIn;
import com.twt.survey.pojo.User;
import com.twt.survey.service.QuestionnaireService;
import com.twt.survey.service.UserService;
import com.twt.survey.utils.JwtUtil;
import com.twt.survey.utils.response.ResultBody;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
public class QuestionnaireController {

    @Resource
    QuestionnaireService questionnaireService;

    @Resource
    UserService userService;

    @Resource
    JwtUtil jwtUtil;

    @PostMapping("/survey")
    public ResultBody addQuestionnaire (@RequestBody QuestionnaireDTO questionnaireDTO,
                                        @RequestHeader(JwtUtil.TOKEN_HEADER) String token){

        String uid = (String) jwtUtil.getPayload(token).get("uid");
        User user = userService.getById(uid);
        if (user.getQuestionnaireId() != null) {
            return ResultBody.error("您已经填写过问卷", null);
        }

//        String q2 = questionnaireDTO.getQ_2();
//        String q4 = questionnaireDTO.getQ_4();
//        if (q4.contains("群众") || q4.contains("共青团员")) {
//            String q5 = questionnaireDTO.getQ_5();
//            if (StaticUtils.judgeNull(Collections.singletonList(q5))) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//            if (q5.contains("不是")) {
//                List<String> q6 = questionnaireDTO.getQ_6();
//                if (StaticUtils.judgeNull(q6)) {
//                    return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//                }
//            }
//        }
//        if (!q2.contains("2024")) {
//            List<String> list = new ArrayList<>();
//            list.add(questionnaireDTO.getQ_10());
//            list.add(questionnaireDTO.getQ_24_1());
//            list.add(questionnaireDTO.getQ_24_2());
//            list.add(questionnaireDTO.getQ_24_3());
//            list.add(questionnaireDTO.getQ_24_4());
//            list.add(questionnaireDTO.getQ_24_5());
//            list.add(questionnaireDTO.getQ_24_6());
//            list.add(questionnaireDTO.getQ_24_7());
//            list.add(questionnaireDTO.getQ_24_8());
//            list.add(questionnaireDTO.getQ_24_9());
//            list.add(questionnaireDTO.getQ_24_10());
//            if (StaticUtils.judgeNull(list)) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//        }
//        if (q2.contains("2024本")) {
//            List<String> list = new ArrayList<>();
//            list.add(questionnaireDTO.getQ_29_1());
//            list.add(questionnaireDTO.getQ_29_2());
//            list.addAll(questionnaireDTO.getQ_30());
//            list.addAll(questionnaireDTO.getQ_31());
//            list.add(questionnaireDTO.getQ_32_1());
//            list.add(questionnaireDTO.getQ_32_2());
//            list.add(questionnaireDTO.getQ_32_3());
//            list.add(questionnaireDTO.getQ_32_4());
//            list.add(questionnaireDTO.getQ_32_5());
//            list.add(questionnaireDTO.getQ_32_6());
//            list.add(questionnaireDTO.getQ_32_7());
//            list.add(questionnaireDTO.getQ_32_8());
//            list.add(questionnaireDTO.getQ_32_9());
//            list.add(questionnaireDTO.getQ_32_10());
//            list.add(questionnaireDTO.getQ_32_11());
//            list.add(questionnaireDTO.getQ_33_1());
//            list.add(questionnaireDTO.getQ_33_2());
//            list.add(questionnaireDTO.getQ_33_3());
//            list.add(questionnaireDTO.getQ_33_4());
//            list.add(questionnaireDTO.getQ_33_5());
//            list.add(questionnaireDTO.getQ_33_6());
//            list.add(questionnaireDTO.getQ_33_7());
//            if (StaticUtils.judgeNull(list)) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//        }
//        if ((q2.contains("2020") || q2.contains("2021") || q2.contains("2022") || q2.contains("2023") || q2.contains("2024")) && q2.contains("本")) {
//            if (StaticUtils.judgeNull(questionnaireDTO.getQ_35())) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//        }
//        if ((q2.contains("2022") || q2.contains("2023") || q2.contains("2024")) && q2.contains("硕")) {
//            List<String> list = new ArrayList<>(questionnaireDTO.getQ_36());
//            list.add(questionnaireDTO.getQ_37());
//            if (StaticUtils.judgeNull(list)) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//        }
//
//        if ((q2.contains("2022") || q2.contains("2023") || q2.contains("2024")) && q2.contains("本")) {
//            List<String> list = new ArrayList<>();
//            list.add(questionnaireDTO.getQ_49_1());
//            list.add(questionnaireDTO.getQ_49_2());
//            list.add(questionnaireDTO.getQ_49_3());
//            list.add(questionnaireDTO.getQ_49_4());
//            list.add(questionnaireDTO.getQ_49_5());
//            list.add(questionnaireDTO.getQ_49_6());
//            list.add(questionnaireDTO.getQ_49_7());
//            list.add(questionnaireDTO.getQ_49_8());
//            list.add(questionnaireDTO.getQ_49_9());
//            list.add(questionnaireDTO.getQ_49_10());
//            list.add(questionnaireDTO.getQ_49_11());
//            list.add(questionnaireDTO.getQ_49_12());
//
//            list.add(questionnaireDTO.getQ_50_1());
//            list.add(questionnaireDTO.getQ_50_2());
//            list.add(questionnaireDTO.getQ_50_3());
//            list.add(questionnaireDTO.getQ_50_4());
//            list.add(questionnaireDTO.getQ_50_5());
//            list.add(questionnaireDTO.getQ_50_6());
//            list.add(questionnaireDTO.getQ_50_7());
//            list.add(questionnaireDTO.getQ_50_8());
//            list.add(questionnaireDTO.getQ_50_9());
//            list.add(questionnaireDTO.getQ_50_10());
//            list.add(questionnaireDTO.getQ_50_11());
//            list.add(questionnaireDTO.getQ_50_12());
//
//            if (StaticUtils.judgeNull(list)) {
//                return new ResultBody(CodeEnum.UNFINISHED_STATUS);
//            }
//        }

        Questionnaire questionnaire = new Questionnaire(questionnaireDTO);
        questionnaireService.save(questionnaire);
        Integer id = questionnaire.getId();

        user.setQuestionnaireId(id);
        userService.updateById(user);

        return ResultBody.success("填写成功！", null);

    }


    @PostMapping("/signin")
    public ResultBody login (@RequestBody SignIn signIn) {
        String account = signIn.getAccount();
        String password = signIn.getPassword();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("account", account);
        paramMap.put("password", password);

        HttpRequest loginRequest = HttpRequest.post("https://api.twt.edu.cn/api/auth/common")
                .header("domain", "weipeiyang.twt.edu.cn")
                .header("ticket", "YmFuYW5hLjM3YjU5MDA2M2Q1OTM3MTY0MDVhMmM1YTM4MmIxMTMwYjI4YmY4YTc=")
                .header("Cookie", "JSESSIONID=824387D685F213C214C3E1A67F68DF64")
                .form(paramMap);

        JSONObject jsonBody = JSONUtil.parseObj(loginRequest.execute().body(), JSONConfig.create().isIgnoreNullValue());

        ResultBody demo = new ResultBody();
        demo.setCode(String.valueOf(jsonBody.getInt("error_code")));
        demo.setMessage(jsonBody.getStr("message"));
        demo.setResult(jsonBody.get("result"));
        User user = new User();
        if (Objects.equals(demo.getMessage(), "成功")){
            ConverterRegistry converterRegistry = ConverterRegistry.getInstance();
            JSONObject tmp = JSONUtil.parseObj(jsonBody.get("result"));
            String uid = tmp.getStr("userNumber");
            String name = tmp.getStr("realname");
            tmp.remove("userNumber");
            tmp.remove("realname");
            tmp.putOpt("uid", uid);
            tmp.putOpt("name", name);
            user = converterRegistry.convert(User.class, tmp);
            if (userService.getById(user.getUid())==null){
                userService.save(user);
            }
        }else {
            return demo;
        }
        JSONObject r = JSONUtil.parseObj(user);
        String token = jwtUtil.generateToken(user);
        Date expirationDate = jwtUtil.getPayload(token).getExpiration();
        JwtUtil.tokenMap.put(token, expirationDate);
        r.putOpt("token", token);
        return ResultBody.success("success", r);
    }

}
